package com.zhuoli.service.springboot.redis.repository.repository;

import com.zhuoli.service.redis.client.template.redis.RedisTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.List;


@Component
@AllArgsConstructor
@Slf4j
public class RedisZSetRepository {

    private RedisTemplate redisTemplate;

    public List<Pair<Long, Double>> getList(String key, Integer limit) {
        return redisTemplate.execute(jedis -> {
            List<Pair<Long, Double>> ret = new ArrayList<>();
            for (Tuple e : jedis.zrevrangeWithScores(key, 0L, limit - 1)) {
                if (e != null && e.getElement().length() > 0) {
                    ret.add(new ImmutablePair<>(Long.parseLong(e.getElement()), e.getScore()));
                }
            }
            return ret;
        });
    }

    public List<Pair<String, Double>> getListComplex(String key, Integer limit) {
        return redisTemplate.execute(jedis -> {
            List<Pair<String, Double>> ret = new ArrayList<>();
            for (Tuple e : jedis.zrevrangeWithScores(key , 0L, limit - 1)) {
                ret.add(new ImmutablePair<>(e.getElement(), e.getScore()));
            }
            return ret;
        });
    }

    public List<Pair<Long, Double>> getListByScore(String key, Double min, Integer limit) {
        return redisTemplate.execute(jedis -> {
            List<Pair<Long, Double>> ret = new ArrayList<>();
            for (Tuple e : jedis.zrevrangeByScoreWithScores(key, Double.MAX_VALUE, min, 0, limit - 1)) {
                ret.add(new ImmutablePair<>(Long.parseLong(e.getElement()), e.getScore()));
            }
            return ret;
        });
    }

    public List<Pair<String, Double>> getListAll(String key) {
        return redisTemplate.execute(jedis -> {
            List<Pair<String, Double>> ret = new ArrayList<>();
            for (Tuple e : jedis.zrevrangeWithScores(key, 0L, -1L)) {
                ret.add(new ImmutablePair<>(e.getElement(), e.getScore()));
            }
            return ret;
        });
    }

    public Long getListSize(String key) {
        return redisTemplate.execute(jedis -> jedis.zcard(key));
    }

    public List<String> getListMembers(String key, Integer count, Integer offset) {
        return redisTemplate.execute(jedis -> new ArrayList<>(jedis.zrangeByScore(key, "-inf", "+inf", offset, count)));
    }

    public List<Pair<String, Double>> getListComplex(String key, Double min, Integer count, Integer offset) {
        return redisTemplate.execute(jedis -> {
            List<Pair<String, Double>> ret = new ArrayList<>();
            for (Tuple e : jedis.zrevrangeByScoreWithScores(key, Double.MAX_VALUE, min, offset, count)) {
                ret.add(new ImmutablePair<>(e.getElement(), e.getScore()));
            }
            return ret;
        });
    }


}